//
//  HomeWorkListViewController.m
//  ZPIOSEducation
//
//  Created by 董伟1 on 17/1/11.
//  Copyright © 2017年 mini1. All rights reserved.
//

#import "HomeWorkListViewController.h"
#import "HomeWorkListCell.h"
#import "HomeWorkDetailViewController.h"
#import "MJRefresh.h"

@interface HomeWorkListViewController () <UITableViewDelegate,UITableViewDataSource>
{
    NSMutableArray  *dataArray;
    NSInteger  currentPage;
    
    BOOL hasNextPage;
}

@property (weak, nonatomic) IBOutlet UITableView *mainTableView;

@end

@implementation HomeWorkListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    self.title = @"作业";
    [[NSNotificationCenter  defaultCenter] addObserver:self selector:@selector(refreshHomeWorkList) name:@"refreshHomeWorkList" object:nil];
    
    self.mainTableView.delegate = self;
    self.mainTableView.dataSource = self;
    self.mainTableView.tableFooterView = [[UIView  alloc] init];
    
    dataArray = [[NSMutableArray  alloc] init];
    currentPage = 1;
    hasNextPage = NO;
    
    __weak HomeWorkListViewController *weakSelf = self;
    [self.mainTableView  addLegendFooterWithRefreshingBlock:^{
        [weakSelf  loadMoreData];
    }];
    
    [self  requestData];
    
}

- (void)refreshHomeWorkList
{
    currentPage = 1;
    [self  requestData];
}

- (void)loadMoreData
{
    if (hasNextPage) {
        [self  requestData];
    }else {
        [self.mainTableView.footer endRefreshing];
    }
}

- (void)requestData
{
    [self  showHudithString:@"加载中..."];
    NSDictionary  *paramDic = [NSDictionary  dictionaryWithObjectsAndKeys:[[ZPAuthStore  sharedZPAuthStore].userToDic  objectForKey:@"stuId"],@"id",@"10",@"pagesize",[NSString  stringWithFormat:@"%ld",(long)currentPage],@"pagenumber", nil];
    [[ZPNetworking  sharedZPNetworking] POST:APIHome@"home/getHomeworkListDetail.json" parameters:paramDic progress:^(NSProgress * _Nullable progress) {
        
    } success:^(id  _Nonnull responseObject) {
        NSLog(@"======%@",responseObject);
        [self  hiddenHud];
        if (self.mainTableView.footer.isRefreshing) {
            [self.mainTableView.footer  endRefreshing];
        }
        
        if ([[responseObject  objectForKey:@"code"] integerValue] == 200) {
            if (currentPage == 1) {
                [dataArray  removeAllObjects];
            }
            if ([[[responseObject  objectForKey:@"result"] objectForKey:@"result"] isKindOfClass:[NSArray  class]]) {
                [dataArray  addObjectsFromArray:[[responseObject  objectForKey:@"result"] objectForKey:@"result"]];
            }
            [self.mainTableView  reloadData];
            
            if ([[[responseObject  objectForKey:@"result"] objectForKey:@"hasNextPage"] boolValue]) {
                currentPage ++;
                hasNextPage = YES;
            }else {
                hasNextPage = NO;
                [self.mainTableView.footer  noticeNoMoreData];
            }
            
            if (dataArray.count == 0) {
                [self  showNoDataViewWithTitle:@"还没有作业哦~"];
            }else {
                [self  hiddenNoDataView];
            }
            
        }else {
            [ZPPromptView  showMessage:[responseObject  objectForKey:@"codeMsg"]];
        }
        
    } failure:^(NSError * _Nonnull error) {
        [self  hiddenHud];
        if (self.mainTableView.footer.isRefreshing) {
            [self.mainTableView.footer  endRefreshing];
        }
        
    }];
}

#pragma mark - Button Actions
#pragma mark 写作业按钮点击
- (void)statusButtonClick:(id)sender
{
    UIButton  *tempBtn = (UIButton  *)sender;
    NSInteger  tag = tempBtn.tag;
    NSDictionary  *tempDic = [dataArray  objectAtIndex:tag];
    HomeWorkDetailViewController  *viewController = [[HomeWorkDetailViewController  alloc] initWithNibName:@"HomeWorkDetailViewController" bundle:nil];
    viewController.idStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"id"]];
    viewController.typeStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"buttonType"]];
    [self.navigationController  pushViewController:viewController animated:YES];
}

#pragma mark - UITableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [dataArray  count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100.;
}

- (UITableViewCell  *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"HomeWorkListCell";
    HomeWorkListCell  *cell = (HomeWorkListCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeWorkListCell" owner:self options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [cell.statusBtn  addTarget:self action:@selector(statusButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    NSInteger  row = [indexPath  row];
    cell.statusBtn.tag = row;
    
    NSDictionary  *tempDic = [dataArray  objectAtIndex:row];
    cell.nameLab.text = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"dates"]];
    cell.classLab.text = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"className"]];
    cell.detailLab.text = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"homeworkExplanation"]];
    
    cell.statusLab.hidden = YES;
    cell.statusBtn.hidden = YES;
    NSInteger  buttonType = [[tempDic  objectForKey:@"buttonType"] integerValue];
    switch (buttonType) {
        case 1:
            //写作业
            cell.statusBtn.hidden = NO;
            
            break;
        case 2:
            //待评价
            cell.statusLab.hidden = NO;
            cell.statusLab.text = @"待评分";
            
            break;
        case 3:
            //已评分
            cell.statusLab.hidden = NO;
            cell.statusLab.text = [NSString  stringWithFormat:@"%@分",[tempDic  objectForKey:@"score"]];
            
            break;
            
        default:
            break;
    }
    
    if ([[tempDic  objectForKey:@"isPicture"] integerValue] == 1) {
        cell.containImv.hidden = NO;
    }else {
        cell.containImv.hidden = YES;
    }
    
    return cell;
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary  *tempDic = [dataArray  objectAtIndex:indexPath.row];
    HomeWorkDetailViewController  *viewController = [[HomeWorkDetailViewController  alloc] initWithNibName:@"HomeWorkDetailViewController" bundle:nil];
    viewController.idStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"id"]];
    viewController.typeStr = [NSString  stringWithFormat:@"%@",[tempDic  objectForKey:@"buttonType"]];
    [self.navigationController  pushViewController:viewController animated:YES];
}

- (void)dealloc
{
    [[NSNotificationCenter  defaultCenter] removeObserver:self name:@"refreshHomeWorkList" object:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
